package com.mashibing.api.filter.impl;

/**
 * @ClassName:ApiKeyCheckFilter
 * @author:[开发者名称]
 * @Description:[类的功能描述]
 * @Date:2025/9/13/19:32
 * @Version:1.0
 */

import com.mashibing.api.client.BeaconCacheClient;
import com.mashibing.api.filter.CheckFilter;
import com.mashibing.common.constant.CacheConstant;
import com.mashibing.common.enums.ExceptionEnums;
import com.mashibing.common.exception.ApiException;
import com.mashibing.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 校验客户的apikey是否合法
 */
@Component("apikey")
@Slf4j
public class ApiKeyCheckFilter implements CheckFilter {
    @Autowired
    BeaconCacheClient cacheClient;

    @Override
    public void check(StandardSubmit standardSubmit) {
        log.info("【接口模块校验apikey:{}】------------------------",standardSubmit);
        //基于apikey查询客户个人信息
        Map clientBusiness = cacheClient.hGetAll(CacheConstant.CLIENT_BUSINESS+standardSubmit.getApikey());
        //如果null没查到 抛异常
        if (clientBusiness == null || clientBusiness.size() == 0){
            log.info("【接口模块-校验apikey】 非法的apikey = {}",standardSubmit.getApikey());
            throw new ApiException(ExceptionEnums.ERROR_APIKEY);
        }

        //3. 正常封装数据
        standardSubmit.setClientId(Long.parseLong(clientBusiness.get("id") + ""));
        log.info("【接口模块-校验apikey】 查询到客户信息 clientBusiness = {}",clientBusiness);
    }

}
